package org.malred.hotelsystem.activities;

import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

import org.malred.hotelsystem.R;
import org.malred.hotelsystem.activities.admin.ManagerActivity;
import org.malred.hotelsystem.activities.user.UserHomeActivity;
import org.malred.hotelsystem.dao.LoginDao;
import org.malred.hotelsystem.dao.UserDao;
import org.malred.hotelsystem.utils.DBUtil;
import org.malred.hotelsystem.utils.ToolsUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录界面
 */
public class MainActivity extends BaseActivity {
    private static final String TAG = "MainActivity";
    // 用户名edit
    EditText nameEt;
    // 密码edit
    EditText passEt;
    // 登录按钮
    Button loginBtn;
    // 单选按钮
    RadioButton userRb;
    RadioButton adminRb;

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        // 输入框
        nameEt = findViewById(R.id.login_username);
        passEt = findViewById(R.id.login_password);

        // 登录按钮
        loginBtn = findViewById(R.id.login_btn_login);

        // 单选
        userRb = findViewById(R.id.login_rb_user);
        adminRb = findViewById(R.id.login_rb_admin);
    }

    @Override
    protected void initData() {
        // 创建数据库
        DBUtil dbUtil = new DBUtil(MainActivity.this);
        SQLiteDatabase db = dbUtil.getWritableDatabase();

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 在点击时获取才是最新的值
                String nameStr = ToolsUtil.editTxtToStr(nameEt);
                String passStr = ToolsUtil.editTxtToStr(passEt);

                // 清空表单
                nameEt.setText("");
                passEt.setText("");

                // 判空
                if (ToolsUtil.isEmpty(nameStr)) {
                    showToastSync("请输入用户名/工号");
                    return;
                }
                if (ToolsUtil.isEmpty(passStr)) {
                    showToastSync("请输入密码");
                    return;
                }

                // 选择了用户/管理员登录
                if (ToolsUtil.isCheck(userRb)) {
                    Log.i(TAG, "user onClick: " + nameStr + " " + passStr);
                    int res = LoginDao.login(db, nameStr, passStr, "0");
                    if (res == 0) {
                        showToastSync("账号密码错误");
                        return;
                    }
                    // 保存到用户信息本地
                    //获取SharedPreferences对象
                    SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
                    //获取Editor对象的引用
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    //将获取过来的值放入文件
                    editor.putString("u_id",  UserDao.login(db,nameStr,passStr).getId());
                    // 提交数据
                    editor.apply();

                    navigateTo(UserHomeActivity.class);
                    showToastSync("登录用户账号成功");
                } else if (ToolsUtil.isCheck(adminRb)) {
                    Log.i(TAG, "admin onClick: " + nameStr + " " + passStr);
                    int res = LoginDao.login(db, nameStr, passStr, "1");
                    if (res == 0) {
                        showToastSync("账号密码错误");
                        return;
                    }
                    // 保存到用户信息本地
                    //获取SharedPreferences对象
                    SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
                    //获取Editor对象的引用
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    //将获取过来的值放入文件
                    editor.putString("u_id",  UserDao.login(db,nameStr,passStr).getId());
                    // 提交数据
                    editor.apply();

                    Map<String, String> args = new HashMap<String, String>();
                    args.put("username", nameStr);
                    navigateTo(ManagerActivity.class, args);
                    showToastSync("登录管理账号成功");
                }
            }
        });
    }
}